const { app, Logger } = require('jj.js');
const { exec } = require('child_process');

// 定义服务器启动后自动打开的URL
const url = 'http://localhost:3003';

// 定义一个函数来在默认浏览器中打开URL
function openBrowser(url) {
    switch (process.platform) {
        case 'darwin': // macOS
            exec('open ' + url);
            break;
        case 'win32': // Windows
            exec('start ' + url);
            break;
        case 'linux': // Linux，可能需要根据桌面环境调整
            const xdgOpen = 'xdg-open';
            const gnomeOpen = 'gnome-open';
            exec(`which ${xdgOpen} && ${xdgOpen} ${url} || ${gnomeOpen} ${url}`);
            break;
        default:
            Logger.warn('Unsupported platform');
    }
}

// 启动服务器
app.run(3003, '0.0.0.0', function(err) {
    if (!err) {
        Logger.info('http server is ready on 3003');
        console.log('首页地址：' + url)
        console.log('安装地址：' + url + '/install' )
        openBrowser(url); // 服务器启动后调用此函数
    } else {
        Logger.error('Failed to start the server:', err);
    }
});